
var IconSprite = cc.Sprite.extend({
        disappearAction : null,
    tap_start_pos : {x:0, y:0},
    tap_gap : {x:0, y:0},
    tapping : 0,
//    index_info : {x:0, y:0},
    index_x : 0,
    index_y : 0,
    onEnter : function() {
            this.addTouchEventListense();
            this._super(); 

            this.disappearAction = this.createDisappearAction();
            this.disappearAction.retain(); 
    },

    onExit : function() {
            this.disappearAction.release();
            this._super();
    },

    //添加点击事件
    addTouchEventListense : function() {
            this.touchListener = cc.EventListener.create({
                    event : cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: false,
            onTouchBegan: this.onTouchBegan,
            onTouchMoved : this.onTouchMoved,
            onTouchEnded : this.onTouchEnded,
            });
            cc.eventManager.addListener(this.touchListener, this);
    },

    onTouchBegan : function (touch, event) {
            var pos = touch.getLocation();
            var target = event.getCurrentTarget();
            if (target.getParent().can_move == 0){
                    return false; 
            }

            if (cc.rectContainsPoint(target.getBoundingBox(), pos)) {

                    console.log("touched");
                    target.setZOrder(100);
                    //target.runDisappear(target);
                    target.tap_start_pos.x = pos.x;
                    target.tap_start_pos.y = pos.y;

                    target.tap_gap.x = pos.x - target.getPositionX();
                    target.tap_gap.y = pos.y - target.getPositionY();
                    

                    target.tapping = 1; 
                    target.getParent().can_move = 0;        //锁定移动
                    return true;
            }
    },

    onTouchMoved : function (touch, event) {
            var target = event.getCurrentTarget();
            var pos = touch.getLocation();

            console.log(target.index_x + " " + target.index_y);
            if (target.tapping == 1){
                    
                    target.x = pos.x - target.tap_gap.x;
                    target.y = pos.y - target.tap_gap.y; 

                    //检查是否可以触发交换
                    var dx = pos.x - target.tap_start_pos.x;
                    var dy = pos.y - target.tap_start_pos.y;
                    //if (Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)) > target.getParent().icon_size / 2) {
                    if (Math.abs(dx) > target.getParent().icon_size / 2 || Math.abs(dy) > target.getParent().icon_size / 2) {
                            //触发交换
                            if (Math.abs(dx) > Math.abs(dy)){
                                    //x 方向上的动作
                                    if (dx > 0){
                                            //右边
                                            if (target.index_x < target.getParent().x_count -1 ){
                                                    //开始右移动
                                                    //cc.eventManager.removeListeners(target); 
                                                    target.getParent().replaceRight(target.index_x, target.index_y);
                                                    target.tapping = 0; 
                                                    target.setZOrder(10);
                                            }
                                    }
                                    else {
                                            //左移
                                            if (target.index_x > 0){
                                                    //开始左移动
                                                    //cc.eventManager.removeListeners(target); 
                                                    target.getParent().replaceLeft(target.index_x, target.index_y);
                                                    target.tapping = 0; 
                                                    target.setZOrder(10);
                                            } 
                                    }
                            }
                            else {
                                    //y方向上的动作
                                    if (dy > 0){
                                            if (target.index_y < target.getParent().y_count - 1) {
                                                    //开始上移动
                                                    //cc.eventManager.removeListeners(target); 
                                                    target.getParent().replaceTop(target.index_x, target.index_y);
                                                    target.tapping = 0; 
                                                    target.setZOrder(10);
                                            }
                                    }
                                    else {
                                            if (target.index_y > 0){
                                                    //开始下移动
                                                    //rcc.eventManager.removeListeners(target); 
                                                    target.getParent().replaceDown(target.index_x, target.index_y);
                                                    target.tapping = 0; 
                                                    target.setZOrder(10);
                                            } 
                                    }


                            }
                    } 
            } 
    },

    onTouchEnded : function (touch, event) {
            var target = event.getCurrentTarget();
            if (target.tapping == 1){
                    //cc.eventManager.removeListeners(target); 
                    target.getParent().goBack(target.index_x, target.index_y);
                    target.tapping = 0;
                    target.setZOrder(10);
            } 
    },

    runDisappear : function(target) { 
            console.log("runDisappear...");
            target.setZOrder(100);

            target.stopAllActions();
            //定义消失动画
            var ac = target.disappearAction;
            var seqAc = cc.Sequence.create(ac, cc.CallFunc.create(function() {
                    console.log("callfun....");

                    target.removeFromParent(); 
            }, target));

            target.runAction(seqAc);
    },

    createDisappearAction : function() { 
            var size = cc.winSize;
            var x = cc.random0To1() * size.width;
            var action = cc.MoveTo.create(0.1, cc.p(x, size.height + 30));
            return action;
    },

    removeTouchEventListenser : function(){
            cc.eventManager.removeListener(this.touchListener);
    },

});



